package com.example.douyin;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.PersistableBundle;
import android.provider.Settings;

import java.util.ArrayList;
import java.util.List;

public class HomeActivity extends AppCompatActivity {
    String[] permissions = {Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.CAMERA};
    List<String> mPermissionList = new ArrayList<>();
    int requestCode = 200;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        if (Build.VERSION.SDK_INT >= 23) {
            initPermission();
        } else {
            handler.sendEmptyMessageDelayed(100, 2000);
        }
    }

    private void initPermission() {
        mPermissionList.clear();
        for (int i = 0; i < permissions.length; i++) {
            int res = ActivityCompat.checkSelfPermission(this, permissions[i]);
            if (res != PackageManager.PERMISSION_GRANTED) {
                mPermissionList.add(permissions[i]);
            }
        }
        if (mPermissionList.size() > 0) {
            ActivityCompat.requestPermissions(this, permissions, requestCode);
        } else {
            handler.sendEmptyMessageDelayed(100, 2000);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        boolean flag=false;
        if (requestCode==this.requestCode) {
            for (int i = 0; i <grantResults.length ; i++) {
                if (grantResults[i]==-1) {
                    flag=true;
                }
            }
            if (flag) {
                showSettingDialog();
            }else {
                handler.sendEmptyMessageDelayed(100,2000);
            }
        }
    }

    private void showSettingDialog() {
        AlertDialog.Builder builder=new AlertDialog.Builder(this);
        builder.setTitle("提示信息").setMessage("没有提供权限，无法打开页面，是否跳转设置界面打开权限？")
                .setIcon(R.mipmap.icon)
                .setNegativeButton("取消",null)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Intent intent=new Intent(Settings.ACTION_APPLICATION_DEVELOPMENT_SETTINGS);
                Uri uri=Uri.fromParts("package",HomeActivity.this.getPackageName(),null);
                intent.setData(uri);
                startActivity(intent);
            }
        });
        builder.create().show();
    }

    Handler handler = new Handler() {
        @SuppressLint("HandlerLeak")
        @Override
        public void handleMessage(@NonNull Message msg) {
            if (msg.what == 100) {
                Intent it = new Intent(HomeActivity.this, MainActivity.class);
                startActivity(it);
                finish();
            }

        }
    };
}